package run;

import bean.business;
import bean.customer;
import bean.movie;
import bean.user;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    /*定义数据存储*/
    public static final List<user> list=new ArrayList<>();
    public static Map<business,List<movie>> allMovie=new HashMap<>();
    public static final Scanner sr=new Scanner(System.in);
    public static user s;
    public static final SimpleDateFormat dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
    static {
        business b=new business();
        b.setLoginName("123456");
        b.setPassword("123456");
        b.setUserName("123456");
        b.setSex('男');
        b.setPhone("123456");
        b.setMoney(123456);
        b.setAddress("sb");
        list.add(b);
        customer c=new customer();
        c.setLoginName("1234567");
        c.setPassword("123456");
        c.setUserName("123456");
        c.setSex('男');
        c.setPhone("123456");
        c.setMoney(123456);
        list.add(c);
        List<movie> movies=new ArrayList<>();
        allMovie.put(b,movies);
    }
    public static void main(String[] args) {
        /*首页*/
        show();

    }

    private static void show() {
        while (true) {
            System.out.println("------首页------");
            System.out.println("1.登录");
            System.out.println("2.注册商家");
            System.out.println("3.注册用户");
            String search=sr.nextLine();
            switch (search){
                case "1":login();break;
                case "2":
                    case "3":
                        default:
                        System.out.println("输入指令出错");break;
            }
        }
    }
    /*登录*/
    private static void login() {
        while (true) {
            System.out.println("输入您的账号");
            String loginName=sr.nextLine();
            System.out.println("请您输入密码");
            String password=sr.nextLine();
            /*查询*/
            user u=getUser(loginName);
            if(u!=null){
                if(u.getPassword().equals(password)){
                    System.out.println("登录成功");
                    s=u;
                    if(s instanceof customer){
                        /*普通用户*/
                        showCustomer();
                    }else {
                        /*商家*/
                        showBusiness();
                    }
                    return;
                }else {
                    System.out.println("密码错误");
                }
            }else {
                System.out.println("查无此用户");
            }
        }
    }
/*商家界面*/
    private static void showBusiness() {
        while (true) {
            System.out.println("----欢迎"+s.getUserName()+"商家用户----");
            System.out.println("请您选择以下操作");
            System.out.println("1.展示详情");
            System.out.println("2.上架电影");
            System.out.println("3.下架电影");
            System.out.println("4.修改电影");
            System.out.println("5.退出登录");
            String search=sr.nextLine();
            switch (search) {
                case "1":showAllInformation();break;
                case "2":addMovie();break;
                case "3":deleteMovie();break;
                case "4":updateMovie();break;
                case "5":
                    System.out.println("欢迎下次登录");return;
                default:
                    System.out.println("您输入的操作不对");
                    break;
            }
        }

    }

    private static void updateMovie() {
        business b=(business)s;
        List<movie> movies=allMovie.get(b);
        showAllInformation();
        System.out.println("输入您修改电影的名称");
        String movieName=sr.nextLine();
        movie mov= findMovie(movieName);
        if(mov!=null){
            movies.remove(mov);
            System.out.println("输入修改后片名");
            mov.setName(sr.nextLine());
            System.out.println("输入修改后主演");
            mov.setActor(sr.nextLine());
            System.out.println("输入修改后时长");
            mov.setTime(Double.parseDouble(sr.nextLine()));
            System.out.println("输入修改后价格");
            mov.setPrice(Double.parseDouble(sr.nextLine()));
            System.out.println("输入修改后票的数量");
            mov.setNumber(Integer.parseInt(sr.nextLine()));
            System.out.println("输入修改后开始时间");
            while (true) {
                try {
                    mov.setStartTime(dateTime.parse(sr.nextLine()));
                    break;
                } catch (ParseException e) {
                    System.out.println("时间错误请重新输入");
                    System.out.println("格式为"+dateTime.toString());
                }
            }
            movies.add(mov);
            System.out.println("修改成功");
        }else {
            System.out.println("暂无上架电影无法删除");
        }
    }

    /*查询电影*/
    public static movie findMovie(String movieName){
        if(s instanceof business) {
            business b = (business) s;
            List<movie> movies = allMovie.get(b);
            for (movie movi : movies) {
                if (movi.getName().contains(movieName)) {
                    return movi;
                }
            }
            return null;
        }else{
            ArrayList<movie> arr=new ArrayList<>();
            allMovie.forEach((business, movies) -> movies.forEach(movie -> arr.add(movie)));
            for (movie movie : arr) {
                if(movie.getName().equals(movieName)){
                    return movie;
                }
            }
            return null;
        }
    }
    private static void deleteMovie() {
        business b=(business)s;
        List<movie> movies=allMovie.get(b);
        if(movies.size()>0){
            showAllInformation();
            System.out.println("输入删除电影名称");
            String movieName=sr.nextLine();
            movie t1=findMovie(movieName);
            if(t1!=null){
                movies.remove(t1);
                System.out.println("下架成功");
            }else {
                System.out.println("没有该电影名称");
            }
        }else {
            System.out.println("暂无上架电影无法删除");
            return;
        }
    }

    private static void addMovie() {
        business b=(business)s;
        List<movie> movies= allMovie.get(b);
        movie m=new movie();
        System.out.println("输入片名");
        m.setName(sr.nextLine());
        System.out.println("输入主演");
        m.setActor(sr.nextLine());
        System.out.println("输入时长");
        m.setTime(Double.parseDouble(sr.nextLine()));
        System.out.println("输入价格");
        m.setPrice(Double.parseDouble(sr.nextLine()));
        System.out.println("输入票的数量");
        m.setNumber(Integer.parseInt(sr.nextLine()));
        System.out.println("开始时间");
        while (true) {
            try {
                m.setStartTime(dateTime.parse(sr.nextLine()));
                break;
            } catch (ParseException e) {
                System.out.println("时间错误请重新输入");
                System.out.println("格式为"+dateTime.toString());
            }
        }
        movies.add(m);
        System.out.println("上架成功");
    }

    private static void showAllInformation() {
        business b=(business)s;
        List<movie> movies=allMovie.get(b);
        System.out.println("------商家信息-----");
        System.out.println("昵称"+b.getUserName());
        if (movies.size()>0) {
        System.out.println("上架电影:");
            for (int i = 0; i < movies.size(); i++) {
                movie m=movies.get(i);
                System.out.println("名称："+m.getName()+"  评分:"+m.getScore()+"   主演:"+m.getActor()+"   时长:"+m.getTime()+"  价格:"+m.getPrice()+"  剩余数量:0"+m.getNumber()+"  开始时间"+m.getStartTime());
            }
        }else {
            System.out.println("暂无上架电影");
        }
    }


    /*客户界面*/
    private static void showCustomer() {
        while (true) {
            System.out.println("----欢迎"+s.getUserName()+"客户用户----");
            System.out.println("请您选择以下操作");
            System.out.println("1.展示影片信息");
            System.out.println("2.用电影名称查询电影信息");
            System.out.println("3.评分");
            System.out.println("4.买票");
            System.out.println("5.退出登录");
            String search=sr.nextLine();
            switch (search){
                case "1":showAllMovie();break;
                case "2":findAndShowMovie();break;
                case "3":setScore();break;
                case "4":byMovie();break;
                case "5":return;
                default:
                    System.out.println("您输入的操作不对");break;
            }
        }
    }
/*根据电影名查询电影*/
    private static void findAndShowMovie() {
    }

    private static void byMovie() {
        if (movieNumber()>0) {
            System.out.println("-----购票界面-----");
            showAllMovie();
            System.out.println("输入商家名称");
            business bus=findBusiness(sr.nextLine());
            if(bus!=null){
                System.out.println("输入电影名称");
                movie mov=findMovie(sr.nextLine());
                mov.setNumber(mov.getNumber()-1);
                s.setMoney(s.getMoney()-mov.getPrice());
                showAllMovie();
            }else {
                System.out.println("未查询到该商家名称");
            }
        }else {
            System.out.println("目前没有上架电影");
        }
    }
    /*查询商家*/
    public static business findBusiness(String businessName){
        Set<business> all= allMovie.keySet();
        for (business business : all) {
            if(business.getUserName().equals(businessName)){
                return business;
            }
        }
        return null;
    }
    /*打分*/
    private static void setScore() {
        showAllMovie();
        System.out.println("输入您评价的电影");
        movie mov=findMovie(sr.nextLine());
        System.out.println("输入评价的分数");
    }

    private static void showAllMovie() {
        if (movieNumber()>0) {
            System.out.println("上架电影:");
            allMovie.forEach((business, movies) -> {
                for (int i = 0; i < movies.size(); i++) {
                    movie m=movies.get(i);
                    System.out.println("商家名称"+business.getUserName()+"名称："+m.getName()+"  评分:"+m.getScore()+"   主演:"+m.getActor()+"   时长:"+m.getTime()+"  价格:"+m.getPrice()+"  剩余数量:0"+m.getNumber()+"  开始时间"+m.getStartTime());
                }
            });
        }else {
            System.out.println("没有上架的电影");
        }
    }

    private static user getUser(String loginName) {
        for (user user : list) {
            if (user.getLoginName().equals(loginName)){
                return user;
            }
        }
        return null;
    }
    public static int movieNumber(){
        ArrayList<movie> nu=new ArrayList<>();
        allMovie.forEach((business, movies) -> movies.forEach(movie -> nu.add(movie)));
        return nu.size();
    }
}
